package pl.lodz.p.model;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

import pl.lodz.p.model.enums.AccountType;

@Entity(name="User")
public class User {
	
	@Id
	@GeneratedValue
	private int id;
	
	private String login;
	
	private String password;
	
	@Enumerated(EnumType.STRING)
	private AccountType accType;
	
	@OneToMany(mappedBy="user", fetch=FetchType.LAZY, cascade=CascadeType.ALL)
	private List<Patient> patients;
	
	private String firstName;
	
	private String lastName;
	
	@OneToMany(mappedBy="user", fetch=FetchType.LAZY)
	private List<Doctor> doctor;
	
	private boolean active = false;

	@OneToOne(mappedBy="user", cascade=CascadeType.ALL)
	private NotificationConfiguration notificationConfiguration;
	
	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getPesel() {
		return pesel;
	}

	public void setPesel(String pesel) {
		this.pesel = pesel;
	}

	private String pesel;

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public AccountType getAccType() {
		return accType;
	}

	public void setAccType(AccountType accType) {
		this.accType = accType;
	}

	public List<Patient> getPatients() {
		return patients;
	}

	public void setPatients(List<Patient> patients) {
		this.patients = patients;
	}

	/**
	 * @return the active
	 */
	public boolean isActive() {
		return active;
	}

	/**
	 * @param active the active to set
	 */
	public void setActive(boolean active) {
		this.active = active;
	}

	public List<Doctor> getDoctor() {
		return doctor;
	}

	public void setDoctor(List<Doctor> doctor) {
		this.doctor = doctor;
	}

	@Override
	public String toString() {
		return login;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((login == null) ? 0 : login.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (login == null) {
			if (other.login != null)
				return false;
		} else if (!login.equals(other.login))
			return false;
		return true;
	}
	
	public boolean isDoctor(){
		return accType.equals(AccountType.DOCTOR);
	}
	
	public boolean isManager(){
		return accType.equals(AccountType.MANAGER);
	}

	public NotificationConfiguration getNotificationConfiguration() {
		if (this.notificationConfiguration == null){
			this.notificationConfiguration = new NotificationConfiguration(this);
		}
		return notificationConfiguration ;
	}

	public void setNotificationConfiguration(NotificationConfiguration notificationConfiguration) {
		if (notificationConfiguration == null){
			this.notificationConfiguration = new NotificationConfiguration(this);
		}
		this.notificationConfiguration = notificationConfiguration;
	}
}
